package com.shanghaionstar.user;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.TermsConditionRes;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.user.iview.ITermsConditionsView;
import com.shanghaionstar.user.presenter.TermsConditionsPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.H5JSObject;

import java.util.List;

/**
 * Created by 6335yz on 2016/8/22.
 */
@ContentView(R.layout.terms_content_detail)
public class TermsConditionsActivity extends SwipeActivity implements ITermsConditionsView{
    @FindViewById
    private com.shanghaionstar.weight.TopLayout top_layout;
    @FindViewById(R.id.content_detail_progress)
    private ProgressLayout progressView;

    @FindViewById(value = R.id.assistant_detail_title)
    private TextView contentDetailTitle;
    @FindViewById(value = R.id.ps_wv)
    private WebView mPsWv;
    @FindViewById(value = R.id.tp_wv)
    private WebView mTpWv;

    private TermsConditionsPresenter presnter;

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presnter = new TermsConditionsPresenter(getScheduler());
        presnter.setView(this);
//        Bundle bundle = new Bundle();
//        bundle.putString("detailId", "");
//        bundle.putSerializable("categoryType", CategoryType.ONSTAR_SSA);
        presnter.initData(new Bundle());

        top_layout.setTopTitle(R.string.onstar_service);
        top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

//    private void initContentDetailPage(GetContentDetailResponseType contentDetailResponse) {
//        contentDetailTitle.setVisibility(View.GONE);
//        detailWebView.setBackgroundColor(Color.TRANSPARENT);
//        if (!CommonUtil.isNull(contentDetailResponse.getContent())) {
//            String content = contentDetailResponse.getContent().getContent();
//            String contentUrl = contentDetailResponse.getContent().getContentUrl();
//            if(content != null){
//                detailWebView.loadDataWithBaseURL("file:///android_asset/",
//                        content.replaceAll("null", ""), "text/html", "utf-8", "");
//            }else if(contentUrl != null){
//                detailWebView.loadUrl(contentUrl);
//            }
////            String content = contentDetailResponse.getContent().getContent();
////            if (content == null) {
////                return;
////            }
////            detailWebView.loadDataWithBaseURL("file:///android_asset/",
////                    content.replaceAll("null", ""), "text/html", "utf-8", "");
//        }
//        detailWebView.setWillNotDraw(true);
//        detailWebView.setWebViewClient(new WebViewClient() {
//            @SuppressWarnings("deprecation")
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                if (view.getMeasuredHeight() != 0) {
//                    view.clearView();
//                }
//
//                view.setWillNotDraw(false);
//                super.onPageFinished(view, url);
//            }
//
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                String BASEURL = "file:///android_asset/";
//                if (url.contains(BASEURL)) {
//                    url = url.substring(BASEURL.length());
//                    detailWebView.loadUrl("https://" + url);
//                } else {
//                    detailWebView.loadUrl(url);
//                }
//                return true;
//            }
//        });
//        detailWebView.getSettings().setJavaScriptEnabled(true);
//        detailWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//        detailWebView.addJavascriptInterface(new H5JSObject(this), "jsobject");
//    }
    private void initContentDetailPage(List<TermsConditionRes> list) {
        contentDetailTitle.setVisibility(View.GONE);
        mPsWv.setBackgroundColor(Color.TRANSPARENT);
        mTpWv.setBackgroundColor(Color.TRANSPARENT);
        if (list != null && !list.isEmpty() && list.size()>1) {
            mPsWv.loadUrl(list.get(0).getUrl());
            mTpWv.loadUrl(list.get(1).getUrl());
        }
        mPsWv.setWillNotDraw(true);
        mTpWv.setWillNotDraw(true);
        mPsWv.getSettings().setJavaScriptEnabled(true);
        mPsWv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mPsWv.addJavascriptInterface(new H5JSObject(this), "jsobject");
        mTpWv.getSettings().setJavaScriptEnabled(true);
        mTpWv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mTpWv.addJavascriptInterface(new H5JSObject(this), "jsobject");
        mTpWv.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressView.setVisibility(View.VISIBLE);
            }

            @SuppressWarnings("deprecation")
            @Override
            public void onPageFinished(WebView view, String url) {
                if (view.getMeasuredHeight() != 0) {
                    view.clearView();
                }
                progressView.setVisibility(View.GONE);
                view.setWillNotDraw(false);
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mPsWv.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public void onPageFinished(WebView view, String url) {
                if (view.getMeasuredHeight() != 0) {
                    view.clearView();
                }
                progressView.setVisibility(View.GONE);
                view.setWillNotDraw(false);
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

    }

    @Override
    public void setLoading(boolean isLoading) {
        progressView.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(this, e);
    }

    @Override
    public void onReponseSuccess(List<TermsConditionRes> list) {
        initContentDetailPage(list);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPsWv != null) {
            ((ViewGroup) mPsWv.getParent()).removeView(mPsWv);
            mPsWv.removeAllViews();
            mPsWv.destroy();
            mPsWv = null;
        }
        if (mTpWv != null) {
            ((ViewGroup) mTpWv.getParent()).removeView(mTpWv);
            mTpWv.removeAllViews();
            mTpWv.destroy();
            mTpWv = null;
        }
    }

    @Override
    public Activity onActivity() {
        return this;
    }
}
